package thread;

public class SyncDemo {
    private static boolean success1 = false;//记录第一个人是否取款成功
    private static boolean success2 = false;//记录第二个人是否取款成功
    public static void main(String[] args) {
        Bank bank = new Bank();
        while (true){
            Thread t1 = new Thread(){
                public void run(){
                    success1 = bank.getMoney(20000);
                }
            };
            Thread t2 = new Thread(){
                public void run(){
                    success2 = bank.getMoney(20000);
                }
            };
            t1.start();
            t2.start();
            try {
                Thread.sleep(20);
            } catch (InterruptedException e) {
            }
            if (success1 & success2){
                System.out.println("卡BUG成功！！！同时取钱成功");
            }else{
                System.out.println("正常，只有一个人取钱");
                bank.saveAccount(20000);
                success1 = false;
                success2 = false;
            }
        }
    }
}

class Bank{
    private int account = 20000;

    public boolean getMoney(int money){
        int account = getAccount();
        if(account >= money){
            this.account -= money;
            Thread.yield();
            saveAccount(account);
            return true;
        }
        return false;
    }

    public int getAccount(){
        return account;
    }
    public void saveAccount(int account){
        this.account = account;
    }
}